home *** CD-ROM | disk | FTP | other *** search
- /*-------------------------------------------------------------------------------------
- *
- * Simple Sample AOCE Application Framework
- *
- * ©1991-1993 Apple Computer
- *
- -------------------------------------------------------------------------------------*/
- /*
- * commands.c -- called in response to menu commands or appleevents
- *
- * change history:
- *
- * SJF 04/21/93 1.0b2 update to b2
- * SJF 03/01/93 1.0b1 added digital signatures
- * SJF 02/09/93 1.0b1 update to b1
- * SJF 10/13/92 1.0d4 update to a11
- * SJF 09/09/92 1.0d3 update to a9
- * SJF 05/07/92 1.0d2 update to a6
- * SJF 11/06/91 1.0d1 initial coding
- *
- */
-
- #ifndef __OCEMAIL__
- #include <OCEMail.h>
- #endif
-
- #ifndef __OCESTANDARDMAIL__
- #include <OCEStandardMail.h>
- #endif
-
- #ifndef __DIALOGS__
- #include <Dialogs.h>
- #endif
-
- #ifndef __CONTROLS__
- #include <Controls.h>
- #endif
-
- #ifndef __STANDARDFILE__
- #include <StandardFile.h>
- #endif
-
- #ifndef __FILES__
- #include <Files.h>
- #endif
-
- #ifndef __SCRIPT__
- #include <Script.h>
- #endif
-
- #include "const.h"
- #include "strconst.h"
- #include "mymenus.h"
- #include "mytypes.h"
- #include "globals.h"
- #include "utils.h"
- #include "windowstuff.h"
- #include "myaocetypes.h"
- #include "myaoce.h"
-
- #include "commands.h"
-
- /* closes a window */
-
- void CommCloseWindow(WindowPtr window)
- {
- WInfoHndl infoHndl;
- Boolean shouldClose;
-
- if (!IsAppWindow(window))
- return;
-
- infoHndl = GetWindowInfo(window);
-
- if (!CanCloseLetter(window))
- shouldClose = false;
- else {
- if ((**infoHndl).changed)
- shouldClose = WarnOnWindowClose(window);
- else
- shouldClose = true;
- }
-
- if (shouldClose) {
- SendWindowMessage(window,kDeactivateMessage,nil);
- SendWindowMessage(window,kDestroyMessage,nil);
-
- DisposHandleChk(infoHndl);
- DisposeWindow(window);
- }
- else gDone = false; // we might be quitting, so we'd want to abort that
- }
-
-
- /* processes edit menu commands */
-
- void CommEdit(WindowPtr window,short command)
- {
- short msg;
-
- if (!IsAppWindow(window))
- return;
-
- switch (command) {
- case kUndoItem:
- msg = kUndoMessage;
- break;
- case kCutItem:
- msg = kCutMessage;
- break;
- case kCopyItem:
- msg = kCopyMessage;
- break;
- case kPasteItem:
- msg = kPasteMessage;
- break;
- case kClearItem:
- msg = kClearMessage;
- break;
- case kSelectAllItem:
- msg = kSelectAllMessage;
- break;
- default:
- return;
- }
- SendWindowMessage(window,msg,nil);
- }
-
-
- /* handles preference changing */
-
- void CommEditPreferences(void)
- {
- DialogPtr theDialog;
- short iType;
- Handle iHndl;
- Rect iRect;
- short item;
- Boolean *prefSwitchPtr;
- MyPreferences newPreferences;
-
- BlockMove(&gPreferences,&newPreferences,sizeof(MyPreferences));
-
- theDialog = GetNewDialog(kPrefsDialogID,nil,(WindowPtr)-1L);
- MyDrawDefaultButtonOutline(theDialog,ok);
-
- GetDItem(theDialog,kCloseAfterSave,&iType,&iHndl,&iRect);
- SetCtlValue((ControlHandle)iHndl,newPreferences.closeOnSend);
- GetDItem(theDialog,kShowCloseOptions,&iType,&iHndl,&iRect);
- SetCtlValue((ControlHandle)iHndl,newPreferences.closeOptionsDialog);
- GetDItem(theDialog,kExpandMailerCreate,&iType,&iHndl,&iRect);
- SetCtlValue((ControlHandle)iHndl,newPreferences.expandOnCreate);
- GetDItem(theDialog,kExpandMailerOpen,&iType,&iHndl,&iRect);
- SetCtlValue((ControlHandle)iHndl,newPreferences.expandOnOpen);
-
- do {
- ModalDialog(nil,&item);
- if (item>=kCloseAfterSave && item<=kExpandMailerOpen) {
- switch (item) {
- case kCloseAfterSave:
- prefSwitchPtr = &newPreferences.closeOnSend;
- break;
- case kShowCloseOptions:
- prefSwitchPtr = &newPreferences.closeOptionsDialog;
- break;
- case kExpandMailerCreate:
- prefSwitchPtr = &newPreferences.expandOnCreate;
- break;
- case kExpandMailerOpen:
- prefSwitchPtr = &newPreferences.expandOnOpen;
- break;
- }
- *prefSwitchPtr = !(*prefSwitchPtr);
- GetDItem(theDialog,item,&iType,&iHndl,&iRect);
- SetCtlValue((ControlHandle)iHndl,*prefSwitchPtr);
- }
-
- } while (item!=ok && item!=cancel);
-
- if (item==ok)
- BlockMove(&newPreferences,&gPreferences,sizeof(MyPreferences));
-
- DisposeDialog(theDialog);
- }
-
-
- /* processes print commands */
-
- void CommPrint(WindowPtr window)
- {
- if (IsAppWindow(window))
- SendWindowMessage(window,kPrintMessage,nil);
- }
-
-
- /* processes page setup commands */
-
- void CommPageSetup(WindowPtr window)
- {
- WInfoPtr infoPtr;
- char hState;
- Boolean changed;
-
- SetCursor(&qd.arrow);
-
- if (!IsAppWindow(window))
- return;
-
- infoPtr = BeginWindowAccess(window,&hState);
-
- PrOpen();
- changed = PrStlDialog(infoPtr->printRecord);
- if (PrError()!=noErr)
- DoError(PrError());
- PrClose();
-
- if (changed)
- SendWindowMessage(window,kPageSetupMessage,nil);
-
- EndWindowAccess(window,hState);
- }
-
-
- /* show about box */
-
- void CommAbout(void)
- {
- DialogPtr theDlg;
- short item;
-
- theDlg = GetNewDialog(kAboutBoxID,nil,(WindowPtr)-1L);
- ModalDialog(nil,&item);
- DisposeDialog(theDlg);
- }
-
-
- /* new command */
-
- void CommNew(void)
- {
- MakeWindow(kDrawWindow,nil,nil,true);
- }
-
-
- /* open command */
-
- void CommOpen(void)
- {
- StandardFileReply sfReply;
- SFTypeList typeList;
- OSErr err;
- WindowPtr window;
-
- typeList[0] = kDrawingType;
- typeList[1] = kCDLtrMsgType;
-
- StandardGetFile(nil,2,typeList,&sfReply);
- if (sfReply.sfGood) {
- err = LoOpen(true, &sfReply.sfFile, nil,true,&window);
- if (err!=noErr)
- DoError(err);
- }
- }
-
-
- /* low-level open (can be called by aevt) */
-
- OSErr LoOpen(Boolean diskForm, FSSpec *fSpec, LetterSpec *lSpec,Boolean showWindow,WindowPtr *openedWindow)
- {
- WindowPtr window;
- WInfoHndl info;
- short wType;
- OSErr err;
- FInfo fInfo;
- LetterDescriptor letterDesc;
- void *message;
- RString name;
-
- name.charSet = smRoman;
- name.dataLength = 0;
-
- *openedWindow = nil;
-
- if (diskForm) {
- err = FSpGetFInfo(fSpec,&fInfo);
- if (err!=noErr)
- return err;
-
- switch (fInfo.fdType) {
- case kDrawingType:
- wType = kDrawWindow;
- message = fSpec;
- break;
- case kCDLtrMsgType:
- wType = kDrawMailerWindow;
- letterDesc.onDisk = true;
- BlockMove(fSpec,&letterDesc.u.fileSpec,sizeof(FSSpec));
- message = &letterDesc;
- break;
- default:
- return kInternalError;
- }
- }
- else {
- wType = kDrawMailerWindow;
- letterDesc.onDisk = false;
- BlockMove(lSpec,&letterDesc.u.mailboxSpec,sizeof(LetterSpec));
- message = &letterDesc;
- }
-
-
- window = MakeWindow(wType,nil,nil,false);
-
- if (window) {
- info = GetWindowInfo(window);
- SendWindowMessage(window,kLoadMessage,message);
- if (diskForm)
- BlockMove(fSpec->name, name.body - 1, fSpec->name[0] + 1);
- else {
- (**info).saved = false;
- err = SMPGetComponentInfo(window, 1, kSMPRegarding, &name);
- BlockMove(name.body - 1,fSpec->name,name.dataLength+1);
- if (err!=noErr)
- return err;
- }
- SetWTitle(window, name.body - 1);
- BlockMove(fSpec,&(*info)->fileSpec,sizeof(FSSpec));
- if (showWindow)
- ShowWindow(window);
- }
- *openedWindow = window;
- return noErr;
- }
-
-
- /* save command- returns false if user cancelled save */
-
- Boolean CommSaveFile(WindowPtr window)
- {
- WInfoHndl info;
-
- if (!IsAppWindow(window))
- return true;
-
- info = GetWindowInfo(window);
- if (!(**info).changed)
- return true;
-
- if (!(**info).saved)
- return CommSaveAsFile(window);
- else {
- LoSaveFile(window,kSMPSave);
- return true;
- }
- }
-
-
- /* save as command */
-
- Boolean CommSaveAsFile(WindowPtr window)
- {
- WInfoHndl info;
- StandardFileReply sfReply;
- Str255 promptString;
- Str255 defaultFilename;
- StringPtr fileName;
- WindowKind kind;
-
- info = GetWindowInfo(window);
-
- fileName = (**info).fileSpec.name;
- if (fileName[0]==0) {
- GetResString(defaultFilename,kDefaultFilenameID,kDefaultFilename);
- fileName = defaultFilename;
- }
-
- if (!IsAppWindow(window))
- return true;
-
- kind = GetWindowKind(window);
- switch (kind) {
- case kDrawWindow:
- GetResString(promptString,kPromptStringID,kPromptString);
- break;
- case kDrawMailerWindow:
- GetResString(promptString,kSaveLetterAsID,kSaveLetterAs);
- break;
- }
-
- StandardPutFile(promptString,fileName,&sfReply);
- if (sfReply.sfGood) {
- BlockMove(&sfReply.sfFile,&((**info).fileSpec),sizeof(FSSpec));
- LoSaveFile(window,kSMPSaveAs);
- return true;
- }
- else
- return false;
- }
-
-
- /* low-level save file */
-
- void LoSaveFile(WindowPtr window,SMPSaveType how)
- {
- SendWindowMessage(window,kSaveMessage,(void *)how);
- }
-
-
- Boolean WarnOnWindowClose(WindowPtr window)
- {
- short item;
- WInfoPtr infoPtr;
- char hState;
- Boolean returnValue;
- WindowKind kind;
- Str63 documentKind,quitReason;
-
- kind = GetWindowKind(window);
- switch (kind) {
- case kDrawWindow:
- GetResString(documentKind,kDrawingID,kDrawing);
- break;
- case kDrawMailerWindow:
- GetResString(documentKind,kLetterID,kLetter);
- break;
- }
- if (gDone)
- GetResString(quitReason,kQuittingID,kQuitting);
- else
- GetResString(quitReason,kClosingID,kClosing);
-
- infoPtr = BeginWindowAccess(window,&hState);
- ParamText(documentKind,infoPtr->fileSpec.name,quitReason,nil);
- EndWindowAccess(window,hState);
-
- SetCursor(&qd.arrow);
- item = StopAlert(kWarnCloseID,nil);
-
- switch (item) {
- case cancel:
- returnValue = false;
- break;
- case kDontSave:
- returnValue = true;
- break;
- case ok:
- returnValue = CommSaveFile(window);
- break;
- }
- return returnValue;
- }
-